package com.dycong.common.threadUtil.quartzScheduler;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.Properties;

import static java.util.Calendar.*;
import static org.quartz.CalendarIntervalScheduleBuilder.calendarIntervalSchedule;
import static org.quartz.DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule;

/**
 * Created by dycong on 2016/12/14.
 */
public class SimpleTriggerTest {

    public static void main(String[] args) {
        /*todo 创建调度器对象,StdSchedulerFactory本身就是一个连接池!!!,StdSchedulerFactory连接池的初始化方式有schedulerFactory.initialize（）｛Properties()，filename，propertiesStream｝*/
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        try {
            /*1、获得一个scheduler*/
            Scheduler scheduler = schedulerFactory.getScheduler();

            /*2、获得一个jobDetail*/
            JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                    .withIdentity("myJob")
                    .build();
            /*3、获得一个trigger*/
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(100)
                            .withRepeatCount(10))
                    .build();

            Trigger trigger0 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger2", "group")
                    .startAt(new Date(System.currentTimeMillis()))
                    .withSchedule(calendarIntervalSchedule()
                            .withIntervalInSeconds(1))
                    .build();
            Trigger trigger1 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger3", "group")
                    .startAt(new Date(System.currentTimeMillis()))
                    .withSchedule(dailyTimeIntervalSchedule()
                            .startingDailyAt(TimeOfDay.hourAndMinuteOfDay(9, 0))     //第天9：00开始
                            .endingDailyAt(TimeOfDay.hourAndMinuteOfDay(16, 0))          //16：00 结束
                            .onDaysOfTheWeek(MONDAY, TUESDAY, WEDNESDAY, TUESDAY, FRIDAY)       //周一至周五执行
                            .withIntervalInMinutes(1)           //每间隔1小时执行一次
                            .withRepeatCount(100))           //最多重复100次（实际执行100+1次）
                    .build();

            Trigger trigger2 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger3", "group")
                    .startAt(new Date(System.currentTimeMillis()))
                    .withSchedule(dailyTimeIntervalSchedule()
                            .startingDailyAt(TimeOfDay.hourAndMinuteOfDay(21, 0))         //第天9：00开始
                            .endingDailyAfterCount(10)          //每天执行10次，这个方法实际上根据startTimeOfDay+interval*count 算出 endTimeOfDay
                            .onDaysOfTheWeek(MONDAY, TUESDAY, WEDNESDAY, TUESDAY, FRIDAY)           //周一至周五执行
                            .withIntervalInSeconds(1))                    //每间隔1小时执行一次
                    .build();


            Trigger trigger3 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger3", "group")
                    .startAt(new Date(1506477602000L))
                    .build();

            Trigger trigger4 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger4", "group")
                    .startAt(new Date(System.currentTimeMillis() + 1000 * 5))
                    .build();
            /*4、把任务和触发器放到scheduler中*/
            scheduler.scheduleJob(jobDetail, trigger3);
            /*5、开始任务调度*/
            scheduler.start();


            JobDetail jobDetail2 = JobBuilder.newJob(MyJob.class)
                    .withIdentity("myJob2")         //多个 JobDetail 的 Identity 不可相同，否则只会执行一个
                    .build();
            Scheduler scheduler2 = schedulerFactory.getScheduler();
            scheduler2.scheduleJob(jobDetail2, trigger4);
            /*5、开始任务调度*/
            scheduler2.start();

        } catch (SchedulerException ex) {
            System.out.println("main: " + ex);
        }
    }


    /*todo 创建调度器对象,StdSchedulerFactory本身就是一个连接池!!!,StdSchedulerFactory连接池的初始化方式有schedulerFactory.initialize（）｛Properties()，filename，propertiesStream｝*/
    private static Scheduler createScheduler() throws SchedulerException {
        Properties props = new Properties();
        props.put(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
                "org.quartz.simpl.SimpleThreadPool");
        props.put("org.quartz.threadPool.threadCount", "50");       //任务线程个数
        StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
        schedulerFactory.initialize(props);
//        schedulerFactory.initialize(String filename);
//        schedulerFactory.initialize(InputStream propertiesStream);
        return schedulerFactory.getScheduler();
    }

}
